package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.itheima.domain.Member;
import com.itheima.mapper.LoginMapper;
import com.itheima.service.LoginService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: LoginServiceImpl
 * @Author: zhangwendong
 * @Date: 2020/4/24 21:12
 * @Description: 用户登录功能service
 */
@Service(interfaceClass = LoginService.class)
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;
   /*
   * @Description :用户登录检查(service)
   * @Date 21:13 2020/4/24
   **/
    @Override
    public Member check(Map<String, Object> map) {
        String telephone = (String) map.get("telephone");
        //1.检测用户是否注册过
        //调用mapper层,通过手机号验证用户是否注册
        Member member = loginMapper.isRegister(telephone);
        if(member == null){
             member = new Member();
             member.setPhoneNumber(telephone);
             member.setRegTime(new Date());
             //为用户设置默认密码,密码为手机号后六位
            String password = MD5Utils.md5(telephone.substring(6, 11));
            member.setPassword(password);
            //1.1未注册过,完成自动注册(仅保存手机号和注册日期)
            loginMapper.registerMember(member);
            member.setId(member.getId());
        }
     return member;
    }
}
